package com.cloudptt.wp.ui.message.sub.view;

import java.io.File;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BaseActivity;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.http.operator.IHttpOpCb;
import com.mcptt.http.operator.file.OnUploadListener;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created By Chengjunsen on 2018/9/5
 */
public class ImageActivity extends BaseActivity implements OnClickListener,IHttpOpCb,OnUploadListener{
    private String path = null;
    private ImageView mImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN, WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.func_msg_image);
        Button btn= (Button)findViewById(R.id.saveBtn);
        btn.setOnClickListener(this);
        mImageView = (ImageView)findViewById(R.id.image_view);
        path = getIntent().getStringExtra("path");
        resolvImage();
    }

    private void resolvImage() {
        if (path.isEmpty()) {
            return;
        }
        Log.debug(getClass(), "doPostFile path = " + path);
        mImageView.setImageURI(Uri.fromFile(new File(path)));
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.saveBtn)
		{
			uploadFile(new File(path));
		}
	}
	
	private void uploadFile(File file)
	{
		Log.debug(getClass(), "uploadFile file = " + file);
		if(!file.exists())
		{
			Log.debug(getClass(), "uploadFile file no exists file = " + file);
			Toast.makeText(this, "file is no exist " + file, Toast.LENGTH_SHORT).show();
			return;
		}
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		if(product != null)
		{
//			BootStrapSvrInfo info = product.getSysConfig().getBootStrapSvr();
//			String svrIp = info.getServerIp();
//			MsgOperator operator = (MsgOperator)product.getOperator(ProductConst.Func_Msg);
//			long len = file.length();
//			operator.uploadFile(svrIp, file,len, this,this,"");
		}
	}

	@Override
	public void onOpResult(int id, boolean isSuc, SessionInfo info,int cause, String str,File file)
	{
		String result = " failed";
		if(isSuc)
		{
			result = " successful";
		}
		//Toast.makeText(this, "file upload " + result, Toast.LENGTH_SHORT).show();
	}
//	@Override
//	public void onOpResult(boolean isSuc) {
//		// TODO Auto-generated method stub
//		String result = " failed";
//		if(isSuc)
//		{
//			result = " successful";
//		}
//		Toast.makeText(this, "file upload " + result, Toast.LENGTH_SHORT).show();
//	}

	@Override
	 public void onProgress(long bytesRead, long contentLength, boolean done,String remotFilePath,long len) {
		// TODO Auto-generated method stub
		Log.debug(getClass(), "bytesRead:" + bytesRead);
		Log.debug(getClass(), "contentLength:" + contentLength);
		Log.debug(getClass(), "done:" + done);
		Log.debug(getClass(), "remotFilePath:" + remotFilePath);
	};
}
